package com.diana.serviceImpl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.diana.common.PageUtils;
import com.diana.common.Utils;
import com.diana.entity.ManagerLog;
import com.diana.mapper.ManagerLogMapper;
import com.diana.service.ManagerLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author anzhi
 * @since 2019-04-21
 */
@Service
public class ManagerLogServiceImpl extends ServiceImpl<ManagerLogMapper, ManagerLog> implements ManagerLogService {

    @Autowired
    private ManagerLogMapper managerLogMapper;

    /**
     * 添加日志
     * @param map
     * @return
     */
    @Override
    public String putLog(Map<String, Object> map) {
        String managerId = Utils.toString(map.get("managerId"));
        String remark = Utils.toString(map.get("remark"));
        String managerIp = Utils.toString(map.get("managerIp"));
        String param = Utils.toString(map.get("param"));
        ManagerLog logMain = new ManagerLog();
        logMain.setRemark(remark);
        logMain.setManagerId(managerId);
        logMain.setManagerIp(managerIp);
        logMain.setParam(param);
        if(insert(logMain)){
            return Utils.message("0000","操作成功",null);
        }
        return Utils.message("0001","操作失败",null);
    }

    /**
     * 获取日志列表
     * @param map
     * @return
     */
    @Override
    public String getLogList(Map<String, Object> map) {
        String loginName = Utils.toString(map.get("loginName"));
        String remark = Utils.toString(map.get("remark"));
        Integer index = Utils.toInteger(map.get("index"));
        Integer size = Utils.toInteger(map.get("size"));
        String joinsql = "";
        if(!Utils.isNullOrEmpty(loginName)){
            joinsql += " and b.loginName = '"+loginName+"' ";
        }
        if(!Utils.isNullOrEmpty(remark)){
            joinsql += " and a.remark like '%"+remark+"%' ";
        }
        Page page = PageUtils.getPage(index,size);
        List<Map<String, Object>> list = managerLogMapper.getLogList(joinsql, page);
        page.setRecords(list);

        return Utils.message("0000","获取成功",list,page.getTotal());
    }
}
